/**
 * 
 */
package galacticthrone.screen.menu;

import galacticthrone.empire.species.human.HumanImperial;
import galacticthrone.main.GTGame;
import galacticthrone.map.GalacticMap;
import galacticthrone.map.mapgen.Uniform;
import galacticthrone.properties.Paths;
import galacticthrone.screen.BackgroundImage;
import galacticthrone.screen.MouseCursor;
import javagame.core.io.Screen;
import javagame.core.io.keyboard.KeyActionMap;
import javagame.core.main.Time;
import javagame.impl.io.video.gui.Button;
import javagame.impl.io.video.gui.Component;
import javagame.impl.io.video.screen.ClearScreen;

// TODO Javadoc
/**
 * <br>
 *
 * @author Jaco van der Westhuizen
 */
public class MenuScreen extends Screen
{
    static enum Action
    {
        NEW_GAME,
        EXIT_GAME;
    }

    private static final int BUTTONS_INDEX = 2; // See the constructor

    private final BackgroundImage background = new BackgroundImage(Paths.TEXTURES + "Menu/Background1.jpg");
    private final StarField stars = new StarField(0f, .03f, .015f);
    private final MenuButtons buttons = new MenuButtons();
    private final MouseCursor cursor = new MouseCursor(Paths.TEXTURES + "Mouse.png", 32, 4, 28); 

    private Button highlightedButton = null;

    /**
     */
    public MenuScreen()
    {
        super(true);

        this.addScreenRenderer(new ClearScreen());  // 0
        this.addScreenRenderer(background);         // 1
        this.addScreenRenderer(buttons);            // 2
        this.addScreenRenderer(stars);              // 3
        this.addScreenRenderer(cursor);             // 4
    }

    /* (non-Javadoc)
     * @see javagame.core.io.Screen#getKeyActionMap()
     */
    @Override
    public KeyActionMap getKeyActionMap()
    {
        return null;
    }

    /* (non-Javadoc)
     * @see javagame.core.io.Screen#onKeyActionOn(java.lang.Object)
     */
    @Override
    public void onKeyActionOn(Object action)
    {
    }

    /* (non-Javadoc)
     * @see javagame.core.io.Screen#onKeyActionOff(java.lang.Object)
     */
    @Override
    public void onKeyActionOff(Object action)
    {
    }

    /* (non-Javadoc)
     * @see javagame.core.io.Screen#onCharTyped(char)
     */
    @Override
    public void onCharTyped(char character)
    {
        switch (character)
        {
            case 'n':
            case 'N':
            {
                newGameAction();
                break;
            }
            case 'x':
            case 'X':
            {
                exitGameAction();
                break;
            }
        }
    }

    /* (non-Javadoc)
     * @see javagame.core.io.Screen#onMouseDown(int)
     */
    @Override
    public void onMouseDown(int button)
    {
    }

    /* (non-Javadoc)
     * @see javagame.core.io.Screen#onMouseUp(int)
     */
    @Override
    public void onMouseUp(int button)
    {
        if (highlightedButton == null)
            return;

        switch ((Action)highlightedButton.getAction())
        {
            case NEW_GAME:
            {
                newGameAction();
                break;
            }
            case EXIT_GAME:
            {
                exitGameAction();
                break;
            }
        }
    }

    /* (non-Javadoc)
     * @see javagame.core.io.Screen#onMouseWheel(int)
     */
    @Override
    public void onMouseWheel(int diff)
    {
    }

    /* (non-Javadoc)
     * @see javagame.core.io.Screen#onMousePick()
     */
    @Override
    public void onMousePick()
    {
        if ((mouseCache == null) || ((mouseCache.length >= 1) && (mouseCache[0] == BUTTONS_INDEX)))
        {
            // If the selected component is a button from MenuButtons
            Component comp = ((mouseCache != null) && (mouseCache.length >= 2)) ? buttons.getComponent(mouseCache[1]) : null;
            
            if ((comp == null) || ((comp != highlightedButton) && (comp instanceof Button)))
            {
                // Highlight the new
                Button button = (Button)comp;
                
                if (button != null)
                {
                    button.onMouseOver(true);
                }
                
                // Unhighlight the old
                if (highlightedButton != null)
                {
                    highlightedButton.onMouseOver(false);
                }
                
                // Remember which one to unhighlight
                highlightedButton = button;
            }
        }
    }

    /* (non-Javadoc)
     * @see javagame.core.io.Screen#onMouseMove()
     */
    @Override
    public void onMouseMove()
    {
        cursor.setVisible(isMouseOver);
        cursor.moveTo(this.mouseX, this.mouseY);
    }

    /* (non-Javadoc)
     * @see javagame.core.main.time.Temporal#update(javagame.core.main.time.Time)
     */
    @Override
    public void update(Time time)
    {
        super.update(time);
    }

    private void newGameAction()
    {
        GTGame.setGameMap(new GalacticMap(new Uniform(new HumanImperial(), 120, 120, 400)));
        GTGame.get().setState(GTGame.MAP);
    }

    private void exitGameAction()
    {
        GTGame.get().exit();
    }
}
